package com.zhsm.fuxi.mvp.ui.activitys

import android.content.Intent
import android.view.View
import com.hankkin.reading.base.BaseMvpActivity
import com.hankkin.reading.control.UserControl
import com.lljjcoder.Interface.OnCityItemClickListener
import com.lljjcoder.bean.CityBean
import com.lljjcoder.bean.DistrictBean
import com.lljjcoder.bean.ProvinceBean
import com.lljjcoder.citywheel.CityConfig
import com.lljjcoder.style.citypickerview.CityPickerView
import com.zhsm.fuxi.R
import com.zhsm.fuxi.bean.InvoiceDetailResp
import com.zhsm.fuxi.mvp.contract.InvoiceGiveContract
import com.zhsm.fuxi.mvp.presenter.InvoiceGivePresenter
import com.zhsm.paipaiout.baselibrary.common.enable
import com.zhsm.paipaiout.baselibrary.utils.ToastUtils
import kotlinx.android.synthetic.main.activity_invoice_give.*
import kotlinx.android.synthetic.main.layout_head_bar.view.*
import org.jetbrains.anko.sdk25.coroutines.onClick


/**
 * Created by GuZhC on 2018/11/19.
 * describe:开发票
 */
class InvoiceGiveActivity : BaseMvpActivity<InvoiceGivePresenter>(), InvoiceGiveContract.IView {
    var haveLocation = false
    var isQiye = true
    var orders =""
    var mPicker = CityPickerView()
    override fun getLayoutId(): Int {
        return R.layout.activity_invoice_give
    }

    override fun registerPresenter() = InvoiceGivePresenter::class.java

    override fun initView() {
        setMiuiStatusBar()
        setToolBar()
        invoiceGiveTj.enable(invoiceGiveTt) { isBtnEnable() }
        invoiceGiveTj.enable(invoiceGiveSh) { isBtnEnable() }
        invoiceGiveTj.enable(invoiceGiveNr) { isBtnEnable() }
        invoiceGiveTj.enable(invoiceGiveSjr) { isBtnEnable() }
        invoiceGiveTj.enable(invoiceGivePhone) { isBtnEnable() }
        invoiceGiveTj.enable(invoiceGiveXxdz) { isBtnEnable() }
        invoiceGivePhone.setText(UserControl.getCurrentUser()?.mobile)
        invoiceGiveDq.onClick {
            showCityPickerView()
        }
        invoiceGiveQy.onClick {
            invoiceGiveGr.isChecked = !invoiceGiveQy.isChecked
            isQiye()
        }
        invoiceGiveGr.onClick {
            invoiceGiveQy.isChecked = !invoiceGiveGr.isChecked
            isQiye()
        }
        orders = intent.getStringExtra("orders")
        getPresenter().getInvoiceMoney(orders)
        invoiceGiveTj.onClick {
            //1个人 2企业
            val map = HashMap<String, Any>()
            map["orderIds"] = orders
            if (isQiye){
                map["invoiceType"] = 2
                map["invoiceTaxNum"] = invoiceGiveSh.text.toString()
            } else map["invoiceType"] = 1
            map["invoiceTitle"] = invoiceGiveTt.text.toString()
            map["invoiceContent"] = invoiceGiveNr.text.toString()
            map["invoiceAmount"] =invoiceGiveJe.text.toString()
            map["receiverName"] = invoiceGiveSjr.text.toString()
            map["receiverPhone"] = invoiceGivePhone.text.toString()
            map["receiverAddress"] =invoiceGiveDq.text.toString()
            map["receiverAddressDetail"] = invoiceGiveXxdz.text.toString()
            getPresenter().buildInvoice(map)
        }
    }

    override fun setMoney(money: String?, sucess: Boolean) {
        invoiceGiveJe.text = money
        if (sucess)
            invoiceGiveJe.isEnabled = false
        else {
            invoiceGiveJe.isEnabled = true
            invoiceGiveJe.onClick { getPresenter().getInvoiceMoney(orders) }
        }
    }

    override fun buildInvoiceSucess(it: InvoiceDetailResp?) {
        it?.let {
            val intent = Intent(this,InvoiceDetaileActivity::class.java)
            intent.putExtra("invoiceId",it.id.toString())
            startActivity(intent)
            finish()
        }

    }
    override fun initData() {
        super.initData()
        mPicker.init(this)
    }

    private fun showCityPickerView() {
//添加默认的配置，不需要自己定义
        val cityConfig = CityConfig.Builder()
                .title(" ")//标题
                .titleBackgroundColor("#ffffff")//标题栏背景色
                .confirTextColor("#333647")//确认按钮文字颜色
                .confirmText("确认")//确认按钮文字
                .confirmTextSize(14)//确认按钮文字大小
                .cancelTextColor("#9A9AA0")//取消按钮文字颜色
                .cancelText("取消")//取消按钮文字
                .cancelTextSize(14)//取消按钮文字大小
                .showBackground(true)//是否显示半透明背景
                .visibleItemsCount(3)//显示item的数量
                .setLineColor("#E5E5E5")//中间横线的颜色
                .setLineHeigh(1)//中间横线的高度
                .province("四川省")//默认显示的省份
                .city("成都市")//默认显示省份下面的城市
                .district("武侯区")//默认显示省市下面的区县数据
                .drawShadows(false)//滚轮不显示模糊效果
                .build()
        mPicker.setConfig(cityConfig)

//监听选择点击事件及返回结果
        mPicker.setOnCityItemClickListener(object : OnCityItemClickListener() {
            override fun onSelected(province: ProvinceBean?, city: CityBean?, district: DistrictBean?) {
               val lovation = StringBuilder()
                //省份
                if (province != null) {
                    lovation.append(province)
                }
                //城市
                if (city != null) {
                    lovation.append(city)
                }
                //地区
                if (district != null) {
                    lovation.append(district)
                }
                haveLocation = true
                invoiceGiveTj.isEnabled = isBtnEnable()
                invoiceGiveDq.text = lovation.toString()
                invoiceGiveDq.setTextColor(resources.getColor(R.color.text_black))
            }
            override fun onCancel() {
//                ToastUtils.showLongToast(this, "已取消")
            }
        })

        //显示
        mPicker.showCityPicker()
    }

    private fun isQiye() {
        if (invoiceGiveQy.isChecked) {
            isQiye = true
            invoiceGiveShLl.visibility = View.VISIBLE
        } else {
            isQiye = false
            invoiceGiveShLl.visibility = View.GONE
        }
    }


    private fun setToolBar() {
        invoiceGiveHeadBar.imgLift.onClick { finish() }
        invoiceGiveHeadBar.tvTitle.text = "开具纸质发票"
        invoiceGiveHeadBar.textRight.text = "开票说明"
        invoiceGiveHeadBar.textRight.visibility = View.VISIBLE
        invoiceGiveHeadBar.onClick {
            //            startActivity(Intent(this@InvoiceGiveActivity, **::class.java))
            ToastUtils.makeText("开票说明")
        }
    }

    /*
       判断按钮是否可用
    */
    private fun isBtnEnable(): Boolean {
        return if (isQiye) {
            invoiceGiveTt.text.isNullOrEmpty().not() &&
                    invoiceGiveSh.text.isNullOrEmpty().not() &&
                    invoiceGiveNr.text.isNullOrEmpty().not() &&
                    invoiceGiveSjr.text.isNullOrEmpty().not() &&
                    invoiceGivePhone.text.isNullOrEmpty().not() &&
                    invoiceGiveXxdz.text.isNullOrEmpty().not() &&
                    haveLocation && !invoiceGiveJe.isEnabled
        } else {
            invoiceGiveTt.text.isNullOrEmpty().not() &&
                    invoiceGiveNr.text.isNullOrEmpty().not() &&
                    invoiceGiveSjr.text.isNullOrEmpty().not() &&
                    invoiceGivePhone.text.isNullOrEmpty().not() &&
                    invoiceGiveXxdz.text.isNullOrEmpty().not() &&
                    haveLocation && !invoiceGiveJe.isEnabled
        }
    }

}